﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FrontCenter.Models;
using Framework.MVC;
using Account.IService.Entry;
using Identity.IService.Entry;

namespace FrontCenter.Controllers
{
    public class UserCenterController : AsyncControllerBase
    {
        #region Field

        private readonly static Random m_random = new Random();

        #endregion Feild

        #region Normal

        public ActionResult Normal()
        {
            return View();
        }

        #endregion Normal

        #region Mobile

        public ActionResult Mobile()
        {
            return View();
        }

        #endregion Mobile

        #region Login

        public ActionResult Login()
        {
            return View();
        }

        #endregion Login

        #region Step1

        public ActionResult Step1()
        {
            return View();
        }

        #endregion Step1

        #region Step2
        public void Step2Async(string code) {
            ViewData["code"] = code;
            var model = new ValidateIdentityModel()
            {
                IdentityCode = code,
                Source = Request.UserHostAddress,
                IdentityResource = "getpassword"
            };
            ViewData["identitycode"] = "nonull";
            InitModel(model);
        }

        public ActionResult Step2Completed(IdentityEntry entry)
        {
            if (!string.IsNullOrWhiteSpace(ViewData["code"] as string) && entry == null)
            {
                return RedirectToAction("step1");
            }else{
                ViewData["code"] = entry!=null? entry.code:null ;
                return View();
            }
        }

        #endregion Step2

        #region Step3Data

        public void Step3DataAsync(string code) {
            QueryAnswerModel model = new QueryAnswerModel()
            {
                IdentityCode=code,
                Source=Request.UserHostAddress,
                AnswerSize=-1
            };
            ViewData["code"] = code;
            InitModel(model);
        }
        public ActionResult Step3DataCompleted(Account.IService.Entry.QueryResult<Account.IService.Entry.ResourceEntity<string>> result)
        {
            ViewData["question"] = (result != null && result.entitys.Length>0) ? result.entitys[m_random.Next(0, result.entitys.Length)].code : null;
            return View();
        }

        #endregion Step3Data

        #region Step3Phone

        public void Step3PhoneAsync(string code)
        {
            GetMobileModel model = new GetMobileModel
            {
                IdentityCode = code,
                IdentitySource = "getpassword",
                Source = Request.UserHostAddress
            };
            ViewData["code"] = code;
            InitModel(model);
        }

        public ActionResult Step3PhoneCompleted(Account.IService.Entry.ValueEntity<string> mobile, string error)
        {
            ViewData["mobile"] = mobile == null ? null : mobile.value;
            ViewData["error"] = error;
            return View();
        }

        #endregion Step3Phone

        #region Step3Mail
        public void Step3MailAsync(string code)
        {
            GetUserMailModel model = new GetUserMailModel
            {
                IdentityCode = code,
                Source = Request.UserHostAddress
            };
            ViewData["code"] = code;
            InitModel(model);
        }

        public ActionResult Step3MailCompleted(UserMailEntry entry)
        {
            if (entry != null && ViewData["code"] != null)
            {
                ViewData["email"] = entry.address;
                return View();
            }
            else
            {
                return RedirectToAction("step1");
            }
        }
       

        #endregion Step3Mail

        #region Step4

        public void Step4Async(string code)
        {
            ViewData["code"] = code;
            var model = new ValidateIdentityModel()
            {
                IdentityCode = code,
                Source = Request.UserHostAddress,
                IdentityResource = "modifypassword"
            };
            ViewData["identitycode"] = "nonull";
            InitModel(model);
        }
        public ActionResult Step4Completed(IdentityEntry entry)
        {
            if (!string.IsNullOrWhiteSpace(ViewData["code"] as string) && entry == null)
            {
                return RedirectToAction("step1");
            }
            else
            {
                ViewData["code"] = entry != null ? entry.code : null;
                return View();
            }
        }

        #endregion Step4

        #region Step5

        public ActionResult Step5()
        {
            return View();
        }

        #endregion Step5

    }
}
